/**
 * Copyright (c) 2009-2012 XTEAM
 * All rights reserved.
 */
/**
 * File：AdvertiseDirective.java
 * History:
 *         2012-7-4: Initially created, Chrise.
 */
package com.xteam.ecsystem.directive;

import static freemarker.template.ObjectWrapper.DEFAULT_WRAPPER;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xteam.ecsystem.base.Constant;
import com.xteam.ecsystem.entities.SysAdvConfig;
import com.xteam.ecsystem.service.front.FrontAdvertiseService;
import com.xteam.ecsystem.utils.DirectiveUtil;

import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;

/**
 * 广告标签。
 * @author Chrise
 */
@Service
public class AdvertiseDirective implements TemplateDirectiveModel {
	
	@Autowired
	private FrontAdvertiseService faService;
	
	/**
	 * @see freemarker.template.TemplateDirectiveModel#execute(freemarker.core.Environment, java.util.Map, freemarker.template.TemplateModel[], freemarker.template.TemplateDirectiveBody)
	 */
	@SuppressWarnings("unchecked")
	public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) 
		throws TemplateException, IOException {
		Integer page = DirectiveUtil.getInteger(params, Constant.PAR_PAGE.value());
		Integer position = DirectiveUtil.getInteger(params, Constant.PAR_POSITION.value());
		
		List<SysAdvConfig> list = this.faService.getAdvertises(page, position);
		env.setVariable(Constant.VAR_ADVLIST.value(), DEFAULT_WRAPPER.wrap(list));
		
		body.render(env.getOut());
	}
}
